home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / stmag < prev    next >
Text File  |  1995-03-20  |  3KB  |  89 lines

  1. %SP_STMAG Short-time magnitude.
  2.  
  3. %       [Y,TSCALE] = SP_STMAG(X,FRAME,OVERLAP,FS) computes
  4. %       the short-time magnitude of X using a frame size
  5. %       of LENGTH and a percentage OVERLAP between successive
  6. %       frames using a rectangular data window.  The sampling
  7. %       frequency is given by FS. The short-time magnitude
  8. %       curve is returned in Y and a time scale corresponding
  9. %       to the end of the data frame is returned in TSCALE.
  10. %       The curve may be displayed with the command
  11. %       'plot(y,tscale)'.
  12. %
  13. %       [Y,TSCALE] = SP_STMAG(X,FRAME,OVERLAP,FS,'WINDOW')
  14. %       windows the data through the specified 'WINDOW' before
  15. %       computing the short-time magnitude.  'WINDOW' can be
  16. %       one of the following: 'hamming', 'hanning', 'bartlett',
  17. %       'blackman' or 'triang'.
  18. %
  19. %       See also: STENG, STZCR, AVSMOOTH, MDSMOOTH
  20.  
  21. %       Matlab original by:
  22. %       LT Dennis W. Brown 7-11-93, DWB 11-11-94
  23. %       Naval Postgraduate School, Monterey, CA
  24. %       May be freely distributed.
  25. %       Not for use in commercial products.
  26.  
  27. %       Ref: Rabiner & Schafer, Digital Processing of Speech
  28. %       Signals, 1978, ss 4.2, pp 120-126.
  29.  
  30. require window
  31.  
  32. stmag = function (x, frame, overlap, fs, win)
  33. {
  34.   local (x, frame, overlap, fs, win)
  35.  
  36.   % default values
  37.   y = [];
  38.   tscale = [];
  39.  
  40.   % must have at least 3 args
  41.   if (nargs < 3)
  42.   {
  43.     error("sp_stmag: Requires first three arguments.");
  44.   }
  45.  
  46.   % percentage must be in range 0-100
  47.   if (overlap < 0 || overlap > 100)
  48.   {
  49.     error("sp_stmag: Overlap percentage must be in range 0-100%");
  50.   }
  51.  
  52.   % figure out if we have a vector
  53.   if (min(size(x)) != 1)
  54.   {
  55.     error("sp_stmag: Input arg \"x\" must be a 1xN or Nx1 vector.");
  56.   }
  57.  
  58.   % work with Nx1 vectors
  59.   x = x[:];
  60.  
  61.   % variables
  62.   Ns = length(x);                         % number of samples
  63.   N = floor(fs * frame);                  % samples-per-frame
  64.   Ndiff = floor(N * (1 - overlap/100));   % samples between windows
  65.   L = floor((Ns-N)/Ndiff);                % number of windows
  66.   y = zeros(L,1);                         % space for answer
  67.   tscale = zeros(L,1);                      % space for time
  68.  
  69.   % data window
  70.   datawindow = ones(N,1);                 % rectangular default
  71.   if (nargs == 5)
  72.   {
  73.     datawindow = window (N, win);
  74.   }
  75.  
  76.   % use the absolute value of x
  77.   x = abs(x);
  78.  
  79.   % windows with overlap
  80.   for (k in 1:L)
  81.   {
  82.     s1 = (k-1) * Ndiff + 1;                 % start of window
  83.     tscale[k;1] = k * Ndiff/fs;
  84.     y[k;1] = sum(x[s1:s1+N-1;1] .* datawindow);
  85.   }
  86.   
  87.   return << tscale=tscale ; y=y >>;
  88. };
  89.